package com.jxdinfo.hussar.cloud.module.example.tjjc.util;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.http.client.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * 实现功能：日期处理工具类
 *
 * @author 作者 king
 * @version 1.0.00 2023-06-14 11:25
 */
public class DateHandleUtil {

    public static Logger log = LoggerFactory.getLogger(DateHandleUtil.class);

    /**
     * 获取当月第一天
     * @return
     */
    public static Date getCurrentMonthFirstDay() {

        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return c.getTime();
    }

    /**
     * 获取当月第一天
     * @return
     */
    public static String getCurrentMonthFirstDayStr() {

        return DateUtils.formatDate(getCurrentMonthFirstDay());
    }

    /**
     * 获取下一天的日期
     * @param date
     * @return
     */
    public static String getAfterOneDay(String date) {

        Date d = DateUtils.parseDate(date);
//        Date n = DateUtils.nDaysAfter(1, d);

        return DateUtils.formatDate(d);
    }

    /**
     * 根据开始结束时间补全日期集合
     * @param startDate
     * @param endDate
     * @param dateList
     * @return
     */
    public static List<String> completionDateList(Date startDate, Date endDate, Set<String> dateList) {

        Set<String> dateSet = new HashSet<>();
        dateSet.addAll(dateList);

        if(startDate != null && endDate != null){

            Set<String> dateFilter = new HashSet<>(dateList);

            AtomicBoolean isTime = new AtomicBoolean(false);
            if(CollectionUtil.isNotEmpty(dateList)) {
                dateList.stream().forEach(item -> {
                    if(item.contains(":")) {
                        isTime.set(true);
                    }
                });
            }

            Calendar c = Calendar.getInstance();
            c.setTime(startDate);

            while (c.getTime().before(endDate)) {

                String dateStr = DateUtils.formatDate(c.getTime());
                if(!dateFilter.contains(dateStr)){
                    if(isTime.get()){
                        String timeStr = DateUtils.formatDate(c.getTime());
                        dateSet.add(timeStr);
                    } else {
                        dateSet.add(dateStr);
                    }
                }
                c.add(Calendar.DAY_OF_MONTH, 1);
            }
        }

        List<String> result = dateSet.stream().sorted().collect(Collectors.toList());

        return result;
    }
}